/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.framework.core.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.osgi.extender.internal.dependencies.startup.ContextState;

import com.cqcis.uip.base.framework.core.ServiceState;
import com.cqcis.uip.base.framework.core.StateManage;

/**
 * 服务状态管理
 * 
 * @author huangmy
 * @date   2008-3-21
 */
public class ServiceStateManage implements StateManage {
    private Map<String, ServiceState> states = new LinkedHashMap<String, ServiceState>();
    
    /**
     * 修改服务状态
     * 
     * @param bundleId
     * @param state
     */
    public synchronized void updateState(long bundleId, ContextState state) {
    	updateState(bundleId, state, null);
    }
    
    /**
     * 修改服务状态
     * 
     * @param bundleId
     * @param state
     * @param message
     */
    public synchronized void updateState(long bundleId, ContextState state, String message) {
    	updateState(bundleId, state, message, null);
    }
    
    public synchronized void updateState(long bundleId, ContextState state, String message, Throwable t) {
    	ServiceState ss = states.get(String.valueOf(bundleId));
    	if (ss != null) {
    		if (ContextState.INTERRUPTED.equals(ss.getState())) {
    			// 已经有 异常 记录, 则不记录
    			return;
    		}
    		
    		ss.setState(state);
    		if (message != null) {
    			ss.setMessage(message);
    		}
    		
    		if (t != null) {
    			ss.setThrowable(t);
    		}
    		
    		if (ContextState.STARTED.equals(state)) {
    			synchronized (ss) {
    				ss.notifyAll();
    			}
    		}
    	}
    }
    
    /**
     * 增加状态
     * 
     * @param bundleId
     * @param state
     */
    public synchronized void addState(long bundleId, ServiceState state) {
    	states.put(String.valueOf(bundleId), state);
    }
    
    /**
     * 删除状态
     * 
     * @param bundleId
     */
    public synchronized void remove(long bundleId) {
    	states.remove(String.valueOf(bundleId));
    }
    
    /**
     * 对应bundle的服务状态
     * 
     * @param bundleId
     * @return
     */
    public synchronized ServiceState getState(long bundleId) {
    	return states.get(String.valueOf(bundleId));
    }
    
    /**
     * 所有的服务状态
     * 
     * @return
     */
    public synchronized List<ServiceState> getStates() {
    	List<ServiceState> list = new ArrayList<ServiceState>();
    	for (Iterator<String> it = states.keySet().iterator(); it.hasNext();) {    		
    		list.add(states.get(it.next()));
    	}
    	
    	return list;
    }
    
    /**
     * 等待某个状态
     * 
     * @param state
     */
    public void waitWithStartedState(long bundleId, long milliseconds) {
    	ServiceState ss = states.get(String.valueOf(bundleId));
    	if (ss != null) {
    		synchronized (ss) {
    			try {
    				if (!ContextState.STARTED.equals(ss.getState())) {
    					ss.wait(milliseconds);
    				}					
				} catch (InterruptedException e) {
				}
			}
    	}
    }
}
